package com.tiantian.subject.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.StrUtil;
import com.tiantian.common.core.domain.ResponseResult;
import com.tiantian.common.core.domain.page.TableDataInfo;
import com.tiantian.common.core.validate.AddGroup;
import com.tiantian.common.core.validate.EditGroup;
import com.tiantian.common.log.annotation.Log;
import com.tiantian.common.log.enums.BusinessType;
import com.tiantian.common.log.enums.OperatorType;
import com.tiantian.common.satoken.domain.controller.BaseController;
import com.tiantian.subject.domain.dto.RecoverSubjectDto;
import com.tiantian.subject.domain.entity.Subject;
import com.tiantian.subject.domain.vo.SubjectOrderVo;
import com.tiantian.subject.domain.vo.SubjectVo;
import com.tiantian.subject.service.SubjectService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * @author tiantian
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/topic")
@Slf4j
@Validated
public class SubjectController extends BaseController {

    private final SubjectService subjectService;

    /**
     * 查询题目列表
     */
    @SaCheckLogin
    @GetMapping("/list")
    public TableDataInfo getSubjectList(Subject subject) {
        return getDataTable(subjectService.getSubjectList(subject));
    }

    /**
     * 分页查询
     */
    @GetMapping("/list/author")
    @SaCheckPermission("setTopic")
    public TableDataInfo selectPageByAuthor(Subject subject) {
        startPage();
        // 设置作者为当前登录用户
        subject.setAuthorId(getUserId());
        List<SubjectVo> topicVOList = subjectService.selectTopicVOList(subject);
        return getDataTable(topicVOList);
    }

    /**
     * 题目回收站
     */
    @GetMapping("/list/author/recycleBin")
    @SaCheckPermission("setTopic")
    public TableDataInfo selectAuthorRecycleBin(Subject subject) {
        startPage();
        // 设置作者为当前登录用户
        subject.setAuthorId(getUserId());
        subject.setDelFlag("1");
        List<SubjectVo> topicVOList = subjectService.selectRecycleBin(subject);
        return getDataTable(topicVOList);
    }

    /**
     * 通过topicId查询题目信息
     */
    @SaCheckLogin
    @GetMapping("/{topicId}")
    public ResponseResult<Subject> selectOneById(@PathVariable String topicId) {
        if (StrUtil.isEmpty(topicId)) return ResponseResult.fail("题目ID不能为空");
        return ResponseResult.ok(subjectService.getSubjectDetail(topicId));
    }

    /**
     * 获取序号使用情况
     */
    @GetMapping("/orderList")
    @SaCheckPermission("setTopic")
    public ResponseResult<List<SubjectOrderVo>> getSubjectOrderList() {
        return ResponseResult.ok(subjectService.getSubjectOrderList());
    }

    /**
     * 设置题目
     */
    @PostMapping("/setting")
    @SaCheckPermission("setTopic")
    @Log(businessType = BusinessType.SET_TOPIC, operatorType = OperatorType.ADMIN)
    public ResponseResult<Map<String, Object>> insert(@RequestBody @Validated(AddGroup.class) Subject subject) {
        return ResponseResult.ok(subjectService.setTopic(subject));
    }

    /**
     * 修改一条数据
     */
    @PutMapping("/update")
    @SaCheckPermission("setTopic")
    @Log(businessType = BusinessType.UPDATE_TOPIC, operatorType = OperatorType.ADMIN)
    public ResponseResult<Boolean> update(@RequestBody @Validated(EditGroup.class) Subject subject) {
        subject.setAuthorId(getUserId());
        return ResponseResult.ok(subjectService.updateById(subject));
    }

    /**
     * 删除一条数据
     */
    @DeleteMapping("/del")
    @SaCheckPermission("setTopic")
    @Log(businessType = BusinessType.DEL_TOPIC_LOGIC, operatorType = OperatorType.ADMIN)
    public ResponseResult<Void> delete(@RequestParam String topicId) {
        if (StrUtil.isEmpty(topicId)) return ResponseResult.fail("题目ID不能为空");
        return toAjax(subjectService.logicalDeleteById(topicId, getUserId()));
    }

    /**
     * 永久删除
     */
    @DeleteMapping("/permanentDelete")
    @SaCheckPermission("setTopic")
    @Log(businessType = BusinessType.DEL_TOPIC_permanent, operatorType = OperatorType.ADMIN)
    public ResponseResult<Void> permanentDelete(@RequestParam String topicId) {
        if (StrUtil.isEmpty(topicId)) return ResponseResult.fail("题目ID不能为空");
        return toAjax(subjectService.deleteById(topicId, getUserId()));
    }


    /**
     * 恢复题目
     */
    @PostMapping("/recover")
    @SaCheckPermission("setTopic")
    @Log(businessType = BusinessType.RECOVER_TOPIC, operatorType = OperatorType.ADMIN)
    public ResponseResult<Void> recoverSubject(@Validated @RequestBody RecoverSubjectDto recoverSubjectDto) {
        int rows = subjectService.recoverSubject(recoverSubjectDto);
        return toAjax(rows);
    }

    /**
     * 获取题目总数
     */
    @GetMapping("/total")
    @SaCheckPermission("reply")
    public ResponseResult<Integer> getTopicTotal() {
        return ResponseResult.ok(subjectService.getSubjectTotal());
    }

}

